﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace SharpDiary.Core.Controls
{
    public partial class ListControlBase : UserControl
    {
        /// <summary>
        /// 当前显示的内容是否为搜索结果。
        /// </summary>
        protected bool _isSearchResult = false;

        /// <summary>
        /// 获取当前显示的内容是否为搜索结果。
        /// </summary>
        public bool IsSearchResult
        {
            get { return _isSearchResult; }
        } 

        private PageButtonGroup _linkedPageButtonGroup;

        public PageButtonGroup LinkedPageButtonGroup
        {
            get { return _linkedPageButtonGroup; }
            set
            {
                if (value == _linkedPageButtonGroup) { return; }

                _linkedPageButtonGroup = value;

                if (_linkedPageButtonGroup != null)
                {
                    _linkedPageButtonGroup.PageNumberChanged += new EventHandler<PageNumberChangedEventArgs>(LinkedPageButtonGroup_PageNumberChanged);
                }
            }
        }

        private List<ListItemControlBase> _items = new List<ListItemControlBase>();

        public List<ListItemControlBase> Items
        {
            get { return _items; }
        }

        private ListItemControlBase _selectedItem;

        public ListItemControlBase SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                if (_selectedItem == value || _items == null || _items.Count >= _selectedItem.TabIndex) { return; }

                _items[_selectedItem.TabIndex].IsSelected = true;
                ItemsContainer.Focus();
            }
        }

        public ListControlBase()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;
        }

        /// <summary>
        /// 搜索。
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <returns>匹配的结果个数</returns>
        public virtual int Search(string keywords)
        {
            _isSearchResult = true;
            return 0;
        }

        /// <summary>
        /// 清空。
        /// </summary>
        protected virtual void Clear()
        {
            _isSearchResult = false;

            ItemsContainer.Controls.Clear();
            _items.Clear();
        }

        /// <summary>
        /// 显示 Items。
        /// </summary>
        public virtual void ShowItems()
        {
            _isSearchResult = false;

            _selectedItem = null;
            _items.Clear();
            ItemsContainer.Controls.Clear();

            //设置总页数
            if (_linkedPageButtonGroup != null) { _linkedPageButtonGroup.PageCount = GetPageCount(); }
        }

        /// <summary>
        /// 获取总页数。
        /// </summary>
        /// <returns></returns>
        protected virtual long GetPageCount() { return 0; }

        /// <summary>
        /// 当前页码发生改变后执行此方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void LinkedPageButtonGroup_PageNumberChanged(object sender, PageNumberChangedEventArgs e)
        {

        }

        /// <summary>
        /// 显示 sql 查询语句结果中包含的项信息。
        /// </summary>
        /// <param name="sql"></param>
        public virtual void ShowItems(string sql, params DbParameter[] parameters)
        {
            _isSearchResult = false;

            _selectedItem = null;
            _items.Clear();
            ItemsContainer.Controls.Clear();

            //设置总页数
            if (_linkedPageButtonGroup != null) { _linkedPageButtonGroup.PageCount = GetPageCount(); }
        }

        /// <summary>
        /// 添加新项。
        /// </summary>
        /// <param name="item"></param>
        protected virtual void AddItem(ListItemControlBase item)
        {
            _items.Add(item);

            item.Dock = DockStyle.Top;
            ItemsContainer.Controls.Add(item);
            item.Selected += new EventHandler<EventArgs>(item_Selected);
        }

        private void item_ItemMouseClick(object sender, MouseEventArgs e)
        {
            ListItemControlBase selected = sender as ListItemControlBase;

            if (selected != null)
            {
                ItemsContainer.Focus();
                ItemsContainer.ScrollControlIntoView(selected);
            }
        }

        private void item_Selected(object sender, EventArgs e)
        {
            if (_selectedItem != null) { _selectedItem.IsSelected = false; }

            ListItemControlBase selected = sender as ListItemControlBase;
            _selectedItem = selected;

            if (selected != null)
            {
                ItemsContainer.Focus();
                ItemsContainer.ScrollControlIntoView(selected);
            }
        }
    }
}
